﻿using JuCloudy.Framework;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Skin.BattleNetStyle
{
    public partial class MainPage : UserControl,JuCloudy.Framework.Interface.ISkin
    {
        public MainPage()
        {
            InitializeComponent(); InitStruCateAppi();
        }

        private bool IsPostBack = false;

        private JuCloudy.Framework.Interface.IStructure Structure =
                    (JuCloudy.Framework.Interface.IStructure)Application.Current.RootVisual;

        private ObservableCollection<JuCloudy.Data.Entity.ViewSysStructureAndCategoryAndAppInfo>
            StruCateAppi { get; set; }

        private ObservableCollection<JuCloudy.Data.Entity.SysStructure> Structures
        {
            get
            {
                ObservableCollection<JuCloudy.Data.Entity.SysStructure> newentities = new ObservableCollection<JuCloudy.Data.Entity.SysStructure>();
                var entities = StruCateAppi.Select(p => new { p.StructureId, p.StructureHeader, p.StructureOrder, p.IsVisable });
                foreach (var entity in entities)
                {
                    newentities.Add(new JuCloudy.Data.Entity.SysStructure()
                    {
                        StructureId = entity.StructureId,
                        StructureHeader = entity.StructureHeader,
                        StructureOrder = entity.StructureOrder,
                        IsVisable = entity.IsVisable
                    });
                }
                return new ObservableCollection<JuCloudy.Data.Entity.SysStructure>(newentities.Distinct(new PropertyComparer<JuCloudy.Data.Entity.SysStructure>("StructureId"))
                    .OrderBy(p => p.StructureOrder));
            }
        }

        private ObservableCollection<JuCloudy.Data.Entity.SysCategory> Categories
        {
            get
            {
                ObservableCollection<JuCloudy.Data.Entity.SysCategory> newentities = new ObservableCollection<JuCloudy.Data.Entity.SysCategory>();
                var entities = StruCateAppi.Select(p => new { p.CategoryId, p.CategoryIntruduction, p.CategoryName, p.CategoryOrder, p.CategoryStructureId, p.CategoryVisable });
                foreach (var entity in entities)
                {
                    newentities.Add(new JuCloudy.Data.Entity.SysCategory()
                    {
                        CategoryId = entity.CategoryId,
                        CategoryIntruduction = entity.CategoryIntruduction,
                        CategoryName = entity.CategoryName,
                        CategoryOrder = entity.CategoryOrder,
                        IsVisable = entity.CategoryVisable,
                        StructureId = entity.CategoryStructureId
                    });
                }
                return new ObservableCollection<JuCloudy.Data.Entity.SysCategory>(newentities.Distinct(new PropertyComparer<JuCloudy.Data.Entity.SysCategory>("CategoryId"))
                    .OrderBy(p => p.CategoryOrder));
            }
        }

        private ObservableCollection<JuCloudy.Data.Entity.SysAppInfo> AppInfos
        {
            get
            {
                ObservableCollection<JuCloudy.Data.Entity.SysAppInfo> newentities = new ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>();
                var entities = StruCateAppi.Select(p => new { p.AppId, p.AppInfoCategoryId, p.AppInfoVisable, p.AppIntroduction, p.AppName });
                foreach (var entity in entities)
                {
                    newentities.Add(new JuCloudy.Data.Entity.SysAppInfo()
                    {
                        AppId = entity.AppId,
                        AppIntroduction = entity.AppIntroduction,
                        AppName = entity.AppName,
                        CategoryId = entity.AppInfoCategoryId,
                        IsVisable = entity.AppInfoVisable
                    });
                }
                return newentities;
            }
        }

        public void InitMenu(int StructureId)
        {
            this.g_menu.Children.Clear();
            foreach (var entity in Categories.TakeWhile(p => p.IsVisable == true && p.StructureId == StructureId))
            {
                JuCloudy.Platform.UserControls.jListBox list = new JuCloudy.Platform.UserControls.jListBox()
                {
                    Margin = new Thickness(0, 0, 1, 0)
                };
                var query = from c in AppInfos
                            where c.CategoryId == entity.CategoryId
                            select c;
                foreach (var q in query)
                {
                    list.Items.Add(new JuCloudy.Platform.UserControls.jListBoxItem()
                    {
                        Content = q.AppName,
                        Tag = q.AppId,
                        Padding = new Thickness(25, 5, 0, 5),
                        Cursor = Cursors.Hand
                    });
                }

                if (query.Count() > 0)
                {
                    list.SelectionChanged += (obj, e) =>
                    {
                        if (e.AddedItems.Count > 0)
                        {
                            var item = e.AddedItems[0] as JuCloudy.Platform.UserControls.jListBoxItem;
                            foreach (var sitems in this.g_menu.Children)
                            {
                                if (sitems is JuCloudy.Platform.UserControls.jListBox && sitems != list)
                                {
                                    ((JuCloudy.Platform.UserControls.jListBox)sitems).SelectedItem = null;
                                }
                            }
                            this.InitApp((int)item.Tag);
                        }
                    };
                    this.g_menu.Children.Add(new TextBlock() { FontSize = 14, Text = entity.CategoryName, Padding = new Thickness(15, 5, 0, 5) });
                    this.g_menu.Children.Add(list);
                }
            }
        }

        private void InitApp(int AppId)
        {
            Structure.ShowLoading();
            Structure.InitializeXap(AppId, this.MainRoot, this.txt_appname, this.txt_appintroduction);
        }

        public void InitStruCateAppi()
        {
            StruCateAppi = (ObservableCollection<JuCloudy.Data.Entity.ViewSysStructureAndCategoryAndAppInfo>)
                JuCloudy.Framework.Storge.Session.Client["StruCateAppi"];
            if (!IsPostBack)
            {
                //载入架构下拉框数据
                InitStructureItems();

                IsPostBack = true;
            }
        }

        public void InitStructureItems()
        {
            Structures.OrderBy(p => p.StructureOrder);
            foreach (var entity in Structures.TakeWhile(p => p.IsVisable == true))
            {
                var item = new ComboBoxItem()
                {
                    Tag = entity.StructureId,
                    Content = entity.StructureHeader
                };
                this.select_Structure.Items.Add(item);
            }
            this.select_Structure.SelectedIndex = 0;

        }

        public UIElement GetStructureSeletorContainer()
        {
            return select_Structure;
        }

        public UIElement GetMenuContainer()
        {
            return g_menu;
        }

        public TextBlock GetBrumbControl()
        {
            return new TextBlock();
        }

        public TextBlock GetAppNameControl()
        {
            return txt_appname;
        }

        public TextBlock GetAppintroduction()
        {
            return txt_appintroduction;
        }

        public Grid GetMainRootContainer()
        {
            return MainRoot;
        }

        public void ShowSettingDialog()
        {
            return;
        }


        public void ShowDefaultRoot()
        {
            throw new NotImplementedException();
        }


        public void ShowCustomRoot(int AppId)
        {
            throw new NotImplementedException();
        }

        public void InitResources(EventHandler OnCompleted)
        {
            throw new NotImplementedException();
        }
    }
}
